home *** CD-ROM | disk | FTP | other *** search
/ Workbench Add-On / Workbench Add-On - Volume 1.iso / BBS-Archive / Dev / GNU-SMALLTALK.lha / Delay.st < prev    next >
Text File  |  1992-02-15  |  4KB  |  167 lines

  1. "======================================================================
  2. |
  3. |   Delay Method Definitions
  4. |
  5.  ======================================================================"
  6.  
  7.  
  8. "======================================================================
  9. |
  10. | Copyright (C) 1990, 1991, 1992 Free Software Foundation, Inc.
  11. | Written by Steve Byrne.
  12. |
  13. | This file is part of GNU Smalltalk.
  14. |
  15. | GNU Smalltalk is free software; you can redistribute it and/or modify it
  16. | under the terms of the GNU General Public License as published by the Free
  17. | Software Foundation; either version 1, or (at your option) any later version.
  18. | GNU Smalltalk is distributed in the hope that it will be useful, but WITHOUT
  19. | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  20. | FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
  21. | details.
  22. | You should have received a copy of the GNU General Public License along with
  23. | GNU Smalltalk; see the file COPYING.  If not, write to the Free Software
  24. | Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  
  25. |
  26.  ======================================================================"
  27.  
  28.  
  29. "
  30. |     Change Log
  31. | ============================================================================
  32. | Author       Date       Change 
  33. | sbb         18 May 91      Actually implemented the thing.
  34. |
  35. | sbyrne     25 Apr 89      created.
  36. |
  37. "
  38.  
  39. Object subclass: #Delay
  40.        instanceVariableNames: 'resumptionTime isRelative'
  41.        classVariableNames: 'DelayQueue DelayTimeout DelayIdle'
  42.        poolDictionaries: ''
  43.        category: nil
  44. !
  45.  
  46. Delay comment: 
  47. 'I am the ultimate agent for frustration in the world.  I cause things to wait 
  48. (typically much more than is appropriate).  When a process sends one of my instances a wait message, that process goes to sleep for the interval specified 
  49. when the instance was created.'
  50. !
  51.        
  52.  
  53. !Delay class methodsFor: 'instance creation'!
  54.  
  55. forMilliseconds: millisecondCount
  56.     ^self new init: millisecondCount isRelative: true
  57. !
  58.  
  59. forSeconds: secondCount
  60.     ^self forMilliseconds: secondCount * 1000
  61. !
  62.  
  63. untilMilliseconds: millisecondCount
  64.     ^self new init: millisecondCount isRelative: false
  65. ! !
  66.  
  67.  
  68.  
  69. !Delay class methodsFor: 'general inquiries'!
  70.  
  71. millisecondClockValue
  72.     ^Time millisecondClockValue
  73. ! !
  74.  
  75.  
  76.  
  77. !Delay class methodsFor: 'initialization'!
  78.  
  79. initialize
  80.     DelayQueue _ SortedCollection sortBlock:
  81.     [ :a :b | (a key) <= (b key) ].
  82.     DelayIdle _ Semaphore forMutualExclusion.
  83.     DelayTimeout _ Semaphore new.
  84.     self startDelayLoop
  85. !
  86.  
  87. startDelayLoop
  88.     [ [ true ] whileTrue: 
  89.       [ DelayTimeout wait.
  90.         DelayIdle critical: 
  91.         [ DelayQueue removeFirst value signal.
  92.           DelayQueue isEmpty
  93.               ifFalse: [ Processor signal: DelayTimeout
  94.                        atMilliseconds: 
  95.                            (DelayQueue first key) 
  96.                      - self millisecondClockValue ]
  97.               ]
  98.         ]
  99.       ] forkAt: Processor timingPriority
  100. ! !
  101.  
  102.  
  103.  
  104. !Delay methodsFor: 'accessing'!
  105.  
  106. resumptionTime
  107.     isRelative
  108.     ifTrue: [ ^Delay millisecondClockValue + resumptionTime ] 
  109.     ifFalse: [ ^resumptionTime ] 
  110. ! !
  111.  
  112.  
  113.  
  114. !Delay methodsFor: 'process delay'!
  115.  
  116. wait
  117.     | elt sem |
  118.     DelayQueue isNil
  119.     ifTrue: [ Delay initialize ].
  120.     sem _ Semaphore new.
  121.     elt _ Association key: self resumptionTime
  122.               value: sem.
  123.     DelayIdle critical: [ DelayQueue add: elt.
  124.               "If we've become the head of the list, we need to
  125.                alter the interrupt time"
  126.               DelayQueue first == elt
  127.                   ifTrue: [ Processor signal: DelayTimeout 
  128.                           atMilliseconds: 
  129.                               (DelayQueue first key) -
  130.                                   Delay millisecondClockValue.
  131.                         ]
  132.                   ].
  133.     sem wait
  134. ! !
  135.  
  136.  
  137.  
  138. !Delay methodsFor: 'comparing'!
  139.  
  140. = aDelay
  141.     ^(isRelative = aDelay isRelative)
  142.     and: [ resumptionTime = aDelay internalResumptionTime ]
  143. !
  144.  
  145. hash
  146.     ^resumptionTime
  147. ! !
  148.  
  149.  
  150.  
  151. !Delay methodsFor: 'private'!
  152.  
  153. init: milliseconds isRelative: aBoolean
  154.     isRelative _ aBoolean.
  155.     resumptionTime _ milliseconds
  156. !
  157.  
  158. isRelative
  159.     ^isRelative
  160. !
  161.  
  162. internalResumptionTime
  163.     ^resumptionTime
  164. ! !
  165.